﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Engine;


namespace WindowsGame1
{
    class Actor
    {
        public Vector2 position;
        public Vector2 direction;
        public Vector2 scale;
        //public Texture2D tex;
        public Animation anim;
        public Gravity gravity;

        public float rotation;
        public float angleVelocity;

       // Scene exit;
        public bool readyToexit = false;

        public void update(int dTime)
        {
            position += direction;//Posicion final sea igual a posicion inicial + direccion

            direction += gravity.myGravity;

            if (position.X + direction.X > 750 || position.X < 0)
             {
                 direction.X *= -1;               
             }
            if(position.Y + direction.Y > 550 || position.Y < 0)
            {
                direction.Y *= -1;
            }

            rotation += angleVelocity;

            anim.update(dTime);
        }

       /* public void isColliding(Actor collision)
        {
            exit.readyToExit = true;
        }*/

        public Vector2[] getColRect()
        {
           /* Rectangle AABB;

            AABB.X = (int)position.X - anim.getRect().Width / 2;
            AABB.Y = (int)position.Y - anim.getRect().Height / 2;

            AABB.Width = anim.getRect().Width;
            AABB.Height = anim.getRect().Height;

            return AABB;*/
            Vector2[] vec = new Vector2[4];

            //Vector2.Dot()
            vec[0] = new Vector2(-anim.getRect().Width / 2, anim.getRect().Height / 2);
            vec[1] = new Vector2(anim.getRect().Width / 2, anim.getRect().Height / 2);
            vec[2] = new Vector2(anim.getRect().Width / 2, -anim.getRect().Height / 2);
            vec[3] = new Vector2(-anim.getRect().Width / 2, -anim.getRect().Height / 2);

            Matrix m = Matrix.CreateRotationZ(rotation);

            vec[0] = Vector2.Transform(vec[0], m);
            vec[1] = Vector2.Transform(vec[1], m);
            vec[2] = Vector2.Transform(vec[2], m);
            vec[3] = Vector2.Transform(vec[3], m);

            vec[0] += position;
            vec[1] += position;
            vec[2] += position;
            vec[3] += position;

            return vec;
        }

        public bool isCollision()
        {
            readyToexit = true;
            return readyToexit;
        }

        public Rectangle getAABB()
        {
            Rectangle getAABB;

            Vector2[] array;

            array = getColRect();

            int minX = (int)array[0].X;
            int minY = (int)array[0].Y;
            int maxX = (int)array[0].X;
            int maxY = (int)array[0].Y;

            for (int i = 1; i < array.Length; i++)
            {
                if (array[i].X > maxX)
                    maxX = (int)array[i].X;
                if (array[i].X < minX)
                    minX = (int)array[i].X;
                if (array[i].Y > maxY)
                    maxY = (int)array[i].Y;
                if (array[i].Y < minY)
                    minY = (int)array[i].Y;
            }

            getAABB = new Rectangle(minX, minY, maxX-minX, maxY-minY);
            
            return getAABB;

            /*
            Vector2 vect1 = new Vector2(-anim.getRect().Width / 2, anim.getRect().Height / 2);
            Vector2 vect2 = new Vector2(anim.getRect().Width / 2, anim.getRect().Height / 2);
            Vector2 vect3 = new Vector2(anim.getRect().Width / 2, -anim.getRect().Height / 2);
            Vector2 vect4 = new Vector2(-anim.getRect().Width / 2, -anim.getRect().Height / 2);

            Matrix m = Matrix.CreateRotationZ(rotation);

            vect1 = Vector2.Transform(vect1, m);
            vect2 = Vector2.Transform(vect2, m);
            vect3 = Vector2.Transform(vect3, m);
            vect4 = Vector2.Transform(vect4, m);

            vect1 += position;
            vect2 += position;
            vect3 += position;
            vect4 += position;

            
            */
        }

        public bool checkCollisionSat(Actor actorCheck1, Actor actorCheck2)
        {
            Vector2[] va = new Vector2[2];
            Vector2[] vb = new Vector2[2];
            Vector2[] ejes = new Vector2[4];
            int proy;

            int minA, maxA, minB, maxB;

            for (int i = 1; i < ejes.Length; i++)
            {
                minA = (int)Vector2.Dot(ejes[i], va[0]);
                maxA = minA;

                for (int j = 1; j < va.Length; j++)
                {
                    proy = (int)Vector2.Dot(ejes[i], va[j]);

                    if (proy < minA)
                    {
                        minA = proy;
                    }
                    if (proy > maxA)
                    {
                        maxA = proy;
                    }
                }
            }

            for (int i = 1; i < ejes.Length; i++)
            {
                minB = (int)Vector2.Dot(ejes[i], vb[0]);
                maxB = minB;

                for (int j = 1; j < vb.Length; j++)
                {
                    proy = (int)Vector2.Dot(ejes[i], vb[j]);

                    if (proy < minB)
                    {
                        minB = proy;
                    }
                    if (proy > maxB)
                    {
                        maxB = proy;
                    }
                }
            }
            if (isCollision())
                return false;
            else
                return false;
        }
    }
}
